layui.use(['layer', 'form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var admin = layui.admin;
    var $ax = layui.ax;

    // 让当前iframe弹层高度适应
    admin.iframeAuto();

    // 监听提交
    form.on('submit(submit-psw)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/mgr/changePwd", function (data) {
            parent.submit = 1;
            Feng.success("修改成功!");
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.setData(data.field);
        ajax.start();

        //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        return false;
    });

    // 添加表单验证方法
    form.verify({
        psw: [
            /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,12}$/, '密码必须6到12位，且必须包含字母和数字'
        ],
        repsw: function (t) {
            if (t !== $('#form-psw input[name=newPassword]').val()) {
                return '两次密码输入不一致';
            }
        }
    });
    $('#closeDialog').click(function () {
        parent.submit = 1;
        layer.alert('因保护用户信息安全，请增强密码强度。<br><span style="font-weight: bold; color: red;">立即修改密码！</span>', {
            time: 5*1000,
            success: function(layero, index){
                var timeNum = this.time/1000, setText = function(start){
                    layer.title('<span class="layui-font-red">'+ (start ? timeNum : --timeNum) + '</span> 秒后自动关闭', index);
                };
                setText(!0);
                this.timer = setInterval(setText, 1000);
                if(timeNum <= 0) clearInterval(this.timer);
            },
            end: function(){
                clearInterval(this.timer);
                admin.closeThisDialog();
            }
        });
    });

});